package com.lss.practice;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lss
 * @description
 * @createDate 2024/12/12-9:48
 */
public class Recursion {
    public static void main(String[] args) {
        List<FileNode> list = new ArrayList<>();
        // 创建根目录
        FileNode root = new FileNode("root", -1,0);
        list.add(root);
        // 创建一些子文件和子目录并添加到根目录
        FileNode documents = new FileNode("Documents", 0,1);
        list.add(documents);
        FileNode pictures = new FileNode("Pictures", 0,2);
        list.add(pictures);
        FileNode downloads = new FileNode("Downloads", 0,3);
        list.add(downloads);
        FileNode file1 = new FileNode("file1.txt", 1,4);
        list.add(file1);
        FileNode file2 = new FileNode("file2.txt", 1,5);
        list.add(file2);
        FileNode picture1 = new FileNode("pic1.jpg", 2,6);
        list.add(picture1);
        FileNode folder1 = new FileNode("folder1", 3,7);
        list.add(folder1);
        FileNode file3 = new FileNode("file3.txt", 3,8);
        list.add(file3);
        // 构建目录结构
//        root.addChild(documents);
//        root.addChild(pictures);
//        root.addChild(downloads);
//        documents.addChild(file1);
//        documents.addChild(folder1);
//        folder1.addChild(file2);
//        pictures.addChild(picture1);
//        downloads.addChild(file3);

        // 从这里开始，您可以编写自己的递归方法来遍历此树
        // 比如，实现一个printDirectory方法来打印整个目录结构
//        buildMenu(list);
//
//        printDirectory(root,3);
        List<FileNode> list1 = buildMenu(list, -1);
        for (FileNode x : list1){
            System.out.println(x);
        }
    }

    /**
     * 根据pid进行递归
     * @param list
     * @param pid
     * @return
     */
    private static List<FileNode> buildMenu(List<FileNode> list,Integer pid){
        List<FileNode> tree = new ArrayList<>();
        for (FileNode node : list){
            if (node.getPid().equals(pid)){
                List<FileNode> children = buildMenu(list,node.getId());
                node.setChildren(children);
                tree.add(node);
            }
        }
        return tree;
    }

    /**
     * 构建目录树根据root节点
     * @param list
     */
    private static void buildMenu(List<FileNode> list){
        for (FileNode fileNode : list){
            if (fileNode.getPid() == -1){
                continue;
            }
            for (FileNode parent : list) {
                if (parent.getId().equals(fileNode.getPid())) {
                    parent.addChild(fileNode);
                    break;
                }
            }
        }
    }


    private static void printDirectory(FileNode node, int level) {
        for (int i = 0; i < level; i++) {
            System.out.print("  "); // 打印缩进
        }
        System.out.println(node.getName()); // 打印当前节点名称
        for (FileNode child : node.getChildren()) {
            printDirectory(child, level + 1); // 递归打印子节点
        }
    }
}


class FileNode {
    @Override
    public String toString() {
        return "FileNode{" +
                "name='" + name + '\'' +
                ", pid=" + pid +
                ", children=" + children +
                ", id=" + id +
                '}';
    }

    private String name; // 文件或目录名
    private Integer pid; // 是否是目录
    private List<FileNode> children = new ArrayList<>(); // 子节点列表
    private Integer id;

    public FileNode(String name, Integer pid,Integer id) {
        this.name = name;
        this.pid = pid;
        this.id = id;
    }

    public void setChildren(List<FileNode> children) {
        this.children = children;
    }

    public Integer getPid() {
        return pid;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Integer isDirectory() {
        return pid;
    }

    public void addChild(FileNode child) {
        children.add(child);
    }

    public List<FileNode> getChildren() {
        return children;
    }
}



